package com.tianji.learning.enums;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianji.learning.entity.po.LearningLesson;

/**
 * 测试枚举中对象转为json
 */

public class testMain {

    public static void main(String[] args) {
        LearningLesson learningLesson = new LearningLesson();
        learningLesson.setCourseId(1L);
        learningLesson.setId(1L);
        learningLesson.setStatus(LessonStatus.LEARNING);
        // 并且这里转换为json的时候只有有值的才能变为json
        System.out.println(JSON.toJSONString(learningLesson));

        // jackson转换器转换
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // 如果属性为null则不转换为json
            objectMapper.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL);
            // 即使有一些属性是没有值的但是还是会转换为json
            String s = objectMapper.writeValueAsString(learningLesson);
            System.out.println(s);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
